<?php

use Codesleeve\Stapler\ORM\StaplerableInterface;
use Codesleeve\Stapler\ORM\EloquentTrait;

class Imageobj extends Eloquent implements StaplerableInterface {

    use EloquentTrait;

    protected $guarded = ['id'];
    protected $fillable = ['title', 'img'];
    public $timestamps = false;

    public function __construct(array $attributes = array()) {
        $this->hasAttachedFile('img', [
            'styles' => [
                'obj_main' => [
                    'dimensions' => '600x600#',
                    'convert_options' => ['quality' => 90]
                ],
                'obj_mini' => [
                    'dimensions' => '300x300#',
                    'convert_options' => ['quality' => 90]
                ],
                'thumbnail' => '160x160#'
            ]
        ]);

        parent::__construct($attributes);
    }

    public function objetos() {
        return $this->belongsToMany('Objeto')->withPivot('position');
    }

    public function getPosition() {
        return $this->objetos()->first()->pivot->position;
    }

    public static function validate($input) {
        $rules = ['file' => 'image|mimes:jpeg|max:2000'];
        return Validator::make($input, $rules);
    }

    public function getEntityId() {
        return $this->objetos()->first()->id;
    }

}
